package com.chris.easymallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chris.common.constant.ProductConstant;
import com.chris.easymallproduct.dto.AttrDto;
import com.chris.easymallproduct.entity.AttrAttrgroupRelationEntity;
import com.chris.easymallproduct.entity.AttrGroupEntity;
import com.chris.easymallproduct.entity.CategoryEntity;

import com.chris.easymallproduct.service.AttrAttrgroupRelationService;
import com.chris.easymallproduct.service.AttrGroupService;
import com.chris.easymallproduct.service.CategoryService;
import com.chris.easymallproduct.vo.AttrInfoVo;
import com.chris.easymallproduct.vo.AttrVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chris.common.utils.PageUtils;
import com.chris.common.utils.Query;

import com.chris.easymallproduct.dao.AttrDao;
import com.chris.easymallproduct.entity.AttrEntity;
import com.chris.easymallproduct.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Attr;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new Query<AttrEntity>().getPageWrapper(params, AttrEntity.class)
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils baseList(Map<String, Object> params, Long id) {
        PageUtils pageUtils;
        if (id == 0) {
            IPage<AttrEntity> page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    new Query<AttrEntity>().getPageWrapper(params, AttrEntity.class)
                            .lambda().eq(AttrEntity::getAttrType, ProductConstant.AttrTypeEnum.BASE.getCode())
            );

            pageUtils = new PageUtils(page);
        } else {
            IPage<AttrEntity> page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    new Query<AttrEntity>().getPageWrapper(params, AttrEntity.class)
                            .lambda()
                            .eq(AttrEntity::getCatelogId, id)
            );
            pageUtils = new PageUtils(page);
        }
        List<?> list = pageUtils.getList();
        List<AttrVo> collect = list.stream().map(item -> {
            AttrVo attrVo = new AttrVo();
            AttrEntity attrEntity = (AttrEntity) item;
            BeanUtils.copyProperties(attrEntity, attrVo);
            Long catelogId = attrVo.getCatelogId();
            CategoryEntity categoryEntity = categoryService.getById(catelogId);
            attrVo.setCatelogName(categoryEntity.getName());
            if (ProductConstant.AttrTypeEnum.BASE.getCode().equals(attrEntity.getAttrType())) {
                Long attrId = attrVo.getAttrId();
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq(AttrAttrgroupRelationEntity::getAttrId, attrId));
                if (attrAttrgroupRelationEntity != null) {
                    Long attrGroupId = attrAttrgroupRelationEntity.getAttrGroupId();
                    if (attrGroupId != null) {
                        AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrGroupId);
                        attrVo.setGroupName(attrGroupEntity.getAttrGroupName());
                    }
                }
            }
            return attrVo;
        }).collect(Collectors.toList());
        pageUtils.setList(collect);
        return pageUtils;
    }

    @Override
    public void save(AttrDto dto) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(dto, attrEntity);
        this.save(attrEntity);
        if (ProductConstant.AttrTypeEnum.BASE.getCode().equals(dto.getAttrType())) {
            Long attrGroupId = dto.getAttrGroupId();
            if (attrGroupId != null) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
                attrAttrgroupRelationEntity.setAttrGroupId(attrGroupId);
                attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
            }
        }
    }

    @Override
    public AttrInfoVo getInfo(Long attrId) {
        AttrEntity attrEntity = this.getById(attrId);
        AttrInfoVo attrInfoVo = new AttrInfoVo();
        if (attrEntity != null) {
            BeanUtils.copyProperties(attrEntity, attrInfoVo);
            List<Long> catelogPaths = new ArrayList<>();
            attrGroupService.getCatelogPath(attrEntity.getCatelogId(), catelogPaths);
            Collections.reverse(catelogPaths);
            attrInfoVo.setCatelogPath(catelogPaths);

            if (ProductConstant.AttrTypeEnum.BASE.getCode().equals(attrEntity.getAttrType())) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId()));
                if (attrAttrgroupRelationEntity != null) {
                    attrInfoVo.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
                }
            }
        }
        return attrInfoVo;
    }

    @Override
    @Transactional
    public void update(AttrDto attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.updateById(attrEntity);
        Long attrId = attrEntity.getAttrId();
        long count = attrAttrgroupRelationService.count(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrId, attrId));
        if (ProductConstant.AttrTypeEnum.BASE.getCode().equals(attrEntity.getAttrType())) {
            if (count == 0) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                attrAttrgroupRelationEntity.setAttrId(attrId);
                attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
                attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
            } else {
                attrAttrgroupRelationService.update(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>()
                        .set(AttrAttrgroupRelationEntity::getAttrGroupId, attr.getAttrGroupId())
                        .eq(AttrAttrgroupRelationEntity::getAttrId, attrId));
            }
        }
    }

    @Override
    public PageUtils saleList(Map<String, Object> params, Long id) {
        PageUtils pageUtils;
        if (id == 0) {
            IPage<AttrEntity> page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    new Query<AttrEntity>().getPageWrapper(params, AttrEntity.class)
                            .lambda().eq(AttrEntity::getAttrType, ProductConstant.AttrTypeEnum.SALE.getCode())
            );
            pageUtils = new PageUtils(page);
        } else {
            IPage<AttrEntity> page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    new Query<AttrEntity>().getPageWrapper(params, AttrEntity.class)
                            .lambda()
                            .eq(AttrEntity::getCatelogId, id)
                            .eq(AttrEntity::getAttrType, ProductConstant.AttrTypeEnum.SALE.getCode())
            );
            pageUtils = new PageUtils(page);
        }
        List<?> list = pageUtils.getList();
        List<AttrVo> collect = list.stream().map(item -> {
            AttrVo attrVo = new AttrVo();
            AttrEntity attrEntity = (AttrEntity) item;
            BeanUtils.copyProperties(attrEntity, attrVo);
            Long catelogId = attrVo.getCatelogId();
            CategoryEntity categoryEntity = categoryService.getById(catelogId);
            attrVo.setCatelogName(categoryEntity.getName());
            return attrVo;
        }).collect(Collectors.toList());
        pageUtils.setList(collect);
        return pageUtils;
    }

}